<?php

class Action
{
    public function index() {

        $from = $to = $search_filter = null;

        extract(URLTracker::init(array(
            'search_filter' => array(
                'start_date' => $from,
                'end_date' => $to,
                'province_id' => array(),
            ),
        )));

        $province_id = $search_filter['province_id'];

        $PROVINCE_INDEX = area_helper::province_index();

        // 支付时间
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $from = $search_filter['start_date'];
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $to = $search_filter['end_date'];
        }

        if (!$from || !$to) {
            $from = $to = date('Y-m-d', strtotime('-1 day'));
            URLTracker::parse(array(
                'search_filter' => array(
                    'start_date' => $from,
                    'end_date' => $to
                )
            ), 1);
        }

        $filter = ' WHERE 1' .
                  ($from ? ' AND m.`date` >= \'' . $from . '\'' : '') .
                  ($to ? ' AND m.`date` <= \'' . $to . '\'' : '') .
                  (!empty($province_id) ? ' AND m.`province_code` IN (' . join(',', $province_id) . ')' : '');

        // 地区出货,销售额汇总
        $SQL = 'SELECT ' .
               join(',', array(
                   'm.`province_code`',
                   'SUM(m.`real_price`) sum_price',
                   'SUM(m.`goods_num`) sum_num',
               )) .
               ' FROM `metadata_dealer_order_goods_sku_daily` m' .
               $filter .
               (!empty($province_id) ? ' GROUP BY m.`province_code`' : '') .
               ' ORDER BY m.`province_code`';

        $sums = run_sql('metadata_dealer_order_goods_sku_daily', 'getAll', $SQL);

        // 地区订单数
        $P_SQL = '(SELECT ' . join(',', array(
                'm.`province_code`',
                'm.`order_id`',
            )) . ' FROM `metadata_dealer_order_goods_sku_daily` m' . $filter . ' GROUP BY m.`order_id`) d';

        $SQL = 'SELECT ' .
               join(',', array(
                   'd.`province_code`',
                   'COUNT(d.`order_id`) count_order_id'
               )) .
               ' FROM ' .
               $P_SQL .
               (!empty($province_id) ? ' GROUP BY d.`province_code`' : '') .
               ' ORDER BY d.`province_code`';

        $count_order = run_sql('metadata_dealer_order_goods_sku_daily', 'getAll', $SQL);

        // 地区下单合伙人数
        $P_SQL = '(SELECT ' . join(',', array(
                'm.`province_code`',
                'm.`agency_id`',
            )) . ' FROM `metadata_dealer_order_goods_sku_daily` m' . $filter . ' GROUP BY m.`agency_id`) d';

        $SQL = 'SELECT ' .
               join(',', array(
                   'd.`province_code`',
                   'COUNT(d.`agency_id`) count_agency_id'
               )) .
               ' FROM ' .
               $P_SQL .
               (!empty($province_id) ? ' GROUP BY d.`province_code`' : '') .
               ' ORDER BY d.`province_code`';

        $count_agency = run_sql('metadata_dealer_order_goods_sku_daily', 'getAll', $SQL);

        $data = array();
        if (count($sums)) {
            if (empty($province_id)) {
                $tmp = array_merge($sums[0], $count_order[0], $count_agency[0]);
                $tmp['area'] = '全国';
                unset($tmp['province_code']); // 全国的地区码清理掉, 拒绝进入
                $data[] = $tmp;
            } else {
                $sums = array_to_filelds_assoc('province_code', $sums);
                $count_order = array_to_filelds_assoc('province_code', $count_order);
                $count_agency = array_to_filelds_assoc('province_code', $count_agency);
                foreach ($sums as $k => $v) {
                    $tmp = array_merge($sums[$k], $count_order[$k], $count_agency[$k]);
                    $tmp['area'] = $PROVINCE_INDEX[$v['province_code']]['area'];
                    $data[] = $tmp;
                }
            }
        }

        $totals = array(
            'agency' => array_sum(array_column($data, 'count_agency_id')),
            'order' => array_sum(array_column($data, 'count_order_id')),
            'num' => array_sum(array_column($data, 'sum_num')),
            'price' => num_keep_two_round(array_sum(array_column($data, 'sum_price'))),
        );

        Response::assign('totals', $totals);
        Response::assign('data', $data);
        Response::assign('PROVINCE_INDEX', $PROVINCE_INDEX);
        Response::display('admin/region.html');
    }

    public function province() {
        $data = $totals = $from = $to = $search_filter = null;

        extract(URLTracker::init(array(
            'search_filter' => array(
                'start_date' => $from,
                'end_date' => $to,
                'province_id' => array(),
            ),
        )));

        $province_id = $search_filter['province_id'];

        $PROVINCE_INDEX = area_helper::province_index();

        // 支付时间
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $from = $search_filter['start_date'];
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $to = $search_filter['end_date'];
        }

        if (!$from || !$to) {
            $from = $to = date('Y-m-d', strtotime('-1 day'));
            URLTracker::parse(array(
                'search_filter' => array(
                    'start_date' => $from,
                    'end_date' => $to,
                )
            ), 1);
        }

        if (!empty($province_id)) {

            $filter = ' WHERE 1' .
                      ($from ? ' AND m.`date` >= \'' . $from . '\'' : '') .
                      ($to ? ' AND m.`date` <= \'' . $to . '\'' : '') .
                      ' AND m.`province_code` IN (' .
                      join(',', $province_id) .
                      ')';

            // 地区出货,销售额汇总
            $SQL = 'SELECT ' . join(',', array(
                    'm.`agency_id`',
                    'm.`province_code`',
                    'SUM(m.`real_price`) sum_price',
                    'SUM(m.`goods_num`) sum_num',
                )) . ' FROM `metadata_dealer_order_goods_sku_daily` m' . $filter . ' GROUP BY m.`agency_id`';

            $sums = run_sql('metadata_dealer_order_goods_sku_daily', 'getAll', $SQL);

            // 地区订单数
            $P_SQL = '(SELECT ' . join(',', array(
                    'm.`agency_id`',
                    'm.`order_id`',
                )) . ' FROM `metadata_dealer_order_goods_sku_daily` m' . $filter . ' GROUP BY m.`order_id`) d';

            $SQL = 'SELECT ' .
                   join(',', array(
                       'd.`agency_id`',
                       'COUNT(d.`order_id`) count_order_id'
                   )) .
                   ' FROM ' .
                   $P_SQL .
                   (!empty($province_id) ? ' GROUP BY d.`agency_id`' : '') .
                   ' ORDER BY d.`agency_id`';

            $count_order = run_sql('metadata_dealer_order_goods_sku_daily', 'getAll', $SQL);

            $data = array();

            if (count($sums)) {
                $sums = array_to_filelds_assoc('agency_id', $sums);
                $count_order = array_to_filelds_assoc('agency_id', $count_order);

                $agency_ids = array_keys($sums);
                $agency_list = array_to_filelds_assoc('id', _model('user')->getList(array('id' => $agency_ids)));

                foreach ($sums as $k => $v) {
                    $tmp = array_merge($sums[$k], $count_order[$k]);
                    $tmp['area'] = $PROVINCE_INDEX[$v['province_code']]['area'];
                    $tmp['realname'] = $agency_list[$k]['realname'];
                    $data[] = $tmp;
                }
            }

            $totals = array(
                'order' => array_sum(array_column($data, 'count_order_id')),
                'num' => array_sum(array_column($data, 'sum_num')),
                'price' => num_keep_two_round(array_sum(array_column($data, 'sum_price'))),
            );
        }

        Response::assign('totals', $totals);
        Response::assign('data', $data);
        Response::assign('PROVINCE_INDEX', $PROVINCE_INDEX);
        Response::display('admin/province.html');
    }
}